//
//  Book.m
//  ebook
//
//  Created by 吴超飞 on 16/2/23.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "Book.h"

@implementation Book

-(id)initWithDictionary:(NSDictionary *)dic{
    
    self = [super init];
    if (self) {
        NSArray *keys = [dic allKeys];
        //@"lastupdate",@"firstchapterid",@"firstchaptername",@"toptime", @"dpercent", @"rposition", @"fullflag", @"readingtime", @"lasthaptercount", @"chaptercount", @"isinnetstore", @"display",  数据库里用的这些名字，但是接口中用的峰驼命名法，所以需要做判断
        self.bookid = [NSString stringWithFormat:@"%@",dic[[keys containsObject:@"bookid"]?@"bookid":@"id"]];
        self.siteid = [NSString stringWithFormat:@"%@",dic[[keys containsObject:@"siteid"]?@"siteid":@"siteId"]];
        self.bookname = dic[[keys containsObject:@"bookname"]?@"bookname":@"name"];
        self.coverpicurl = dic[[keys containsObject:@"coverpicurl"]?@"coverpicurl":@"image"];
        self.author = dic[@"author"];
        self.updatetime = dic[[keys containsObject:@"lastupdate"]?@"lastupdate":@"updateTime"];
        self.firstchapterid = [NSString stringWithFormat:@"%@",dic[[keys containsObject:@"firstchapterid"]?@"firstchapterid":@"firstChapterId"]];
        self.firstchaptername = dic[[keys containsObject:@"firstchaptername"]?@"firstchaptername":@"firstChapterName"];
        self.toptime = dic[@"toptime"];
        self.chaptercount = [NSString stringWithFormat:@"%@",dic[[keys containsObject:@"chapterCount"]?@"chapterCount":@"chaptercount"]];
        self.dpercent = dic[@"dpercent"];
        self.rposition = dic[@"rposition"];
        self.rchaptername = dic[@"rchaptername"];
        self.fullflag = [NSString stringWithFormat:@"%@",dic[[keys containsObject:@"fullflag"]?@"fullflag":@"fullFlag"]];
        self.readingtime = dic[@"readingtime"];
        self.lasthaptercount = dic[@"lasthaptercount"];
        self.isrecommend = [dic[@"isrecommend"] boolValue];
        self.display = dic[@"display"];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if (self = [super init]) {
        self.bookid = [aDecoder decodeObjectForKey:@"bookid"];
        self.siteid = [aDecoder decodeObjectForKey:@"siteid"];
        self.bookname = [aDecoder decodeObjectForKey:@"bookname"];
        self.coverpicurl = [aDecoder decodeObjectForKey:@"coverpicurl"];
        self.author = [aDecoder decodeObjectForKey:@"author"];
        self.updatetime = [aDecoder decodeObjectForKey:@"updatetime"];
        self.firstchapterid = [aDecoder decodeObjectForKey:@"firstchapterid"];
        self.firstchaptername = [aDecoder decodeObjectForKey:@"firstchaptername"];
        self.toptime = [aDecoder decodeObjectForKey:@"toptime"];
        self.chaptercount = [aDecoder decodeObjectForKey:@"chaptercount"];
        self.dpercent = [aDecoder decodeObjectForKey:@"dpercent"];
        self.rposition = [aDecoder decodeObjectForKey:@"rposition"];
        self.rchaptername = [aDecoder decodeObjectForKey:@"rchaptername"];
        self.fullflag = [aDecoder decodeObjectForKey:@"fullflag"];
        self.readingtime = [aDecoder decodeObjectForKey:@"readingtime"];
        self.lasthaptercount = [aDecoder decodeObjectForKey:@"lasthaptercount"];
        self.isrecommend = [aDecoder decodeBoolForKey:@"isrecommend"];
        self.display = [aDecoder decodeObjectForKey:@"display"];
    }
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeObject:self.bookid forKey:@"bookid"];
    [aCoder encodeObject:self.siteid forKey:@"siteid"];
    [aCoder encodeObject:self.bookname forKey:@"bookname"];
    [aCoder encodeObject:self.coverpicurl forKey:@"coverpicurl"];
    [aCoder encodeObject:self.author forKey:@"author"];
    [aCoder encodeObject:self.updatetime forKey:@"updatetime"];
    [aCoder encodeObject:self.firstchapterid forKey:@"firstchapterid"];
    [aCoder encodeObject:self.firstchaptername forKey:@"firstchaptername"];
    [aCoder encodeObject:self.toptime forKey:@"toptime"];
    [aCoder encodeObject:self.chaptercount forKey:@"chaptercount"];
    [aCoder encodeObject:self.dpercent forKey:@"dpercent"];
    [aCoder encodeObject:self.rposition forKey:@"rposition"];
    [aCoder encodeObject:self.rchaptername forKey:@"rchaptername"];
    [aCoder encodeObject:self.fullflag forKey:@"fullflag"];
    [aCoder encodeObject:self.readingtime forKey:@"readingtime"];
    [aCoder encodeObject:self.lasthaptercount forKey:@"lasthaptercount"];
    [aCoder encodeBool:self.isrecommend forKey:@"isrecommend"];
    [aCoder encodeObject:self.display forKey:@"display"];
}

@end
